package com.ctbri.JWLetter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ctbri.JWLetter.bean.Letter;
import com.ctbri.JWLetter.bean.Tag;
import com.ctbri.JWLetter.dao.es.LetterDao;
import com.ctbri.JWLetter.dao.mapper.TagMapper;
import com.ctbri.JWLetter.frame.Consts;
import com.ctbri.JWLetter.frame.JWJSONObject;
import com.ctbri.JWLetter.service.LetterService;
import com.ctbri.JWLetter.type.error.ErrorCode;
import com.ctbri.JWLetter.util.page.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.annotation.Resource;

/**
 * 信件业务层实现
 * 
 * @author Hogan
 * 
 */
@Service("letterService")
public class LetterServiceImpl implements LetterService {

	@Resource
	private LetterDao letterDao;

	@Resource
	private TagMapper tagMapper;

	@Override
	public JWJSONObject loadAllLetters(JSONObject jParams) {
		JWJSONObject result = new JWJSONObject();
		JSONObject detail = new JSONObject();
		Integer pageNum = jParams.getInteger("pageNum");
		PageResult<Letter> pageResult = new PageResult<Letter>();
		pageResult.setPerPage(Consts.PAGE_SIZE_LETTER);
		pageResult.setCurrentPage(pageNum);
		int startCount = (pageResult.getCurrentPage() - 1) * Consts.PAGE_SIZE_LETTER;
		pageResult.setRecords(letterDao.findLettersByPage(startCount, Consts.PAGE_SIZE_LETTER));
		pageResult.setTotal(letterDao.findLetterCount());
		detail.put("pageResult", pageResult);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setDetail(detail);
		return result;
	}

	@Override
	public JWJSONObject loadTags(JSONObject jParams) {
		JWJSONObject result = new JWJSONObject();
		JSONObject detail = new JSONObject();
		List<Tag> tags = tagMapper.findAllTags();
		detail.put("tags", tags);
		result.setDetail(detail);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}

	@Override
	public JWJSONObject loadLetterCounts(JSONObject jParams) {
		JWJSONObject result = new JWJSONObject();
		JSONObject detail = new JSONObject();
		long letterCount = letterDao.findLetterCount();
		long undoLetterCount = letterDao.findCountByOneParam("readornot", Consts.LETTER_STATUS_UNDO);
		long levelOneLetterCount = letterDao.findCountByOneParam("categoryname", Consts.LETTER_LEVEL_ONE);
		long levelTwoLetterCount = letterDao.findCountByOneParam("categoryname", Consts.LETTER_LEVEL_TWO);
		long levelThreeLetterCount = letterDao.findCountByOneParam("categoryname", Consts.LETTER_LEVEL_THREE);
		detail.put("letterCount", letterCount);
		detail.put("undoLetterCount", undoLetterCount);
		detail.put("levelOneLetterCount", levelOneLetterCount);
		detail.put("levelTwoLetterCount", levelTwoLetterCount);
		detail.put("levelThreeLetterCount", levelThreeLetterCount);
		result.setDetail(detail);
		result.setErrorCode(ErrorCode.SUCCESS);
		return result;
	}

	/**
	 * 加载未读信件
	 * 
	 * @param jParams
	 * @return
	 */
	@Override
	public JWJSONObject loadUndoLetters(JSONObject jParams) {
		JWJSONObject result = new JWJSONObject();
		JSONObject detail = new JSONObject();
		Integer pageNum = jParams.getInteger("pageNum");
		PageResult<Letter> pageResult = new PageResult<Letter>();
		pageResult.setPerPage(Consts.PAGE_SIZE_LETTER);
		pageResult.setCurrentPage(pageNum);
		int startCount = (pageResult.getCurrentPage() - 1) * Consts.PAGE_SIZE_LETTER;
		pageResult.setRecords(letterDao.findLettersByOneParam(startCount, Consts.PAGE_SIZE_LETTER, "readornot",
				Consts.LETTER_STATUS_UNDO));
		pageResult.setTotal(letterDao.findCountByOneParam("readornot", Consts.LETTER_STATUS_UNDO));
		detail.put("pageResult", pageResult);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setDetail(detail);
		return result;

	}

	@Override
	public JWJSONObject loadLevelLetters(JSONObject jParams) {
		JWJSONObject result = new JWJSONObject();
		JSONObject detail = new JSONObject();
		int pageNum = jParams.getInteger("pageNum");
		int level = jParams.getInteger("level");
		PageResult<Letter> pageResult = new PageResult<Letter>();
		pageResult.setPerPage(Consts.PAGE_SIZE_LETTER);
		pageResult.setCurrentPage(pageNum);
		int startCount = (pageResult.getCurrentPage() - 1) * Consts.PAGE_SIZE_LETTER;
		pageResult.setRecords(
				letterDao.findLettersByOneParam(startCount, Consts.PAGE_SIZE_LETTER, "categoryname", level));
		pageResult.setTotal(letterDao.findCountByOneParam("categoryname", level));
		detail.put("pageResult", pageResult);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setDetail(detail);
		return result;
	}

	/**
	 * 加载标签信件
	 *
	 * @param jParams
	 */
	@Override
	public JWJSONObject loadTagLetters(JSONObject jParams) {
		JWJSONObject result = new JWJSONObject();
		JSONObject detail = new JSONObject();
		int pageNum = jParams.getInteger("pageNum");
		String tagName = jParams.getString("tagName");
		PageResult<Letter> pageResult = new PageResult<Letter>();
		pageResult.setPerPage(Consts.PAGE_SIZE_LETTER);
		pageResult.setCurrentPage(pageNum);
		int startCount = (pageResult.getCurrentPage() - 1) * Consts.PAGE_SIZE_LETTER;
		pageResult.setRecords(letterDao.findLettersByMultiParams(startCount, Consts.PAGE_SIZE_LETTER, "tagname", tagName));
		pageResult.setTotal(letterDao.findCountByMultiParams("tagname", tagName));
		detail.put("pageResult", pageResult);
		result.setErrorCode(ErrorCode.SUCCESS);
		result.setDetail(detail);
		return result;
	}

	@Override
	public JWJSONObject loadLetterDetail(JSONObject jParams) {
		JWJSONObject result = new JWJSONObject();
		JSONObject detail = new JSONObject();
		Integer letterId = jParams.getInteger("letterId");
		//if (letterDao.modifyLetterStatus(letterId, Consts.LETTER_STATUS_DONE) == Consts.DATA_HADNLE_SUCCESS) {
			Letter letter = letterDao.findLetterById(letterId);
			letter.getContent().replace("graph?word=", "/JWLetter/forward/graph?word");
			detail.put("letter", letter);
			result.setDetail(detail);
			result.setErrorCode(ErrorCode.SUCCESS);
		//} else {
			//result.setErrorCode(LetterError.MODIFY_STATUS_FAILD);
		//}
		return result;
	}

}
